package net.caixiaomi.info.ui.discovery;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.OpenPrizeAdapter;
import net.caixiaomi.info.adapter.OpenPrizeDetailAdapter;
import net.caixiaomi.info.adapter.OpenPrizeNumAdapter;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.base.progressbar.CircularProgressBar;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.model.ArticleItem;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.DltPrizeEntity;
import net.caixiaomi.info.model.NumberPrizeEntity;
import net.caixiaomi.info.model.OpenPrizeEntity;
import net.caixiaomi.info.model.SzcPrizePageInfo;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.util.Constants;
import net.caixiaomi.smartrefresh.layout.SmartRefreshLayout;
import net.caixiaomi.smartrefresh.layout.api.RefreshLayout;
import net.caixiaomi.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

import static android.R.id.list;

/**
 * Created by mac on 2018/9/6.
 */

public class OpenPrizeNumListActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, OnRefreshListener {

    @BindView(R.id.title)
    TextView mTitle;

    @BindView(list)
    RecyclerView mListView;
    @BindView(R.id.refresh)
    SmartRefreshLayout mRefresh;
    @BindView(R.id.progress)
    CircularProgressBar mProgress;

    private OpenPrizeNumAdapter mAdapter;
    private Context mContext;

    private String mLotteryId;

    private int mPageNum = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLotteryId=getIntent().getStringExtra("lotteryid");
        switch (mLotteryId){
            case "2":
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(getString(R.string.C_BALL_LOTTERY)).append(getString(R.string.C_HISTORY_AWARD));
                mTitle.setText(stringBuilder.toString());
                break;
        }

        findViewById(R.id.frag_group).setFitsSystemWindows(true);
        mContext=this;
        mAdapter=new OpenPrizeNumAdapter(null);
        mRefresh.setOnRefreshListener(this);
        mAdapter.setOnLoadMoreListener(this, mListView);
        mAdapter.setEnableLoadMore(true);
        mListView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter.setOnItemClickListener(this);
        mListView.setAdapter(mAdapter);
        loadData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mPageNum = 1;
        loadData();
    }

    @Override
    protected int layout() {
        return R.layout.frag_home;
    }

    @Override
    public void onLoadMoreRequested() {
        mPageNum++;
        loadData();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        DltPrizeEntity entity= (DltPrizeEntity) adapter.getData().get(position);
        Intent intent=new Intent(mContext, OpenPrizeDetailActivity.class);
        intent.putExtra("lotteryClassify",mLotteryId);
        intent.putExtra("termNum",Integer.parseInt(entity.getTermNum()));
        startActivity(intent);
    }

    private void loadData(){
        JSONObject params = new JSONObject();
        params.put("lotteryClassify", mLotteryId);
        params.put("page", mPageNum);
        params.put("size", Constants.PAGE_SIZE);
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel<SzcPrizePageInfo>> dis = requestService.getDiscoverySZCList(params);
        dis.enqueue(new ResponseCallback<BaseCallModel<SzcPrizePageInfo>>() {
            @Override
            public void onSuccess(BaseCallModel<SzcPrizePageInfo> data) {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
                List<DltPrizeEntity> list = data.data.getSzcPrizePageInfo().getList();
                if (list != null && !list.isEmpty()) {
                    if (mPageNum == 1) {
                        mAdapter.getData().clear();
                        if(list.size()>0) {
                            list.get(0).setType(Constants.ITEM_MATCH_LIVE1);
                        }
                    }
                    mAdapter.addData(list);
                    mAdapter.loadMoreComplete();
                    mAdapter.notifyDataSetChanged();
                    mAdapter.setEnableLoadMore(Boolean.valueOf(data.data.getSzcPrizePageInfo().isHasNextPage()));
                }
            }

            @Override
            public void onFail(ResponseError error) {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
                if (mPageNum != 1) {
                    mAdapter.loadMoreComplete();
                }
                mPageNum = mAdapter.getData().size() % Integer.valueOf(Constants.PAGE_SIZE) == 0 ? mAdapter.getData().size() / Integer.valueOf(Constants.PAGE_SIZE) : mAdapter.getData().size() / Integer.valueOf(Constants.PAGE_SIZE) + 1;

            }

            @Override
            public void onError() {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
                if (mPageNum != 1) {
                    mAdapter.loadMoreComplete();
                }
                mPageNum = mAdapter.getData().size() % Integer.valueOf(Constants.PAGE_SIZE) == 0 ? mAdapter.getData().size() / Integer.valueOf(Constants.PAGE_SIZE) : mAdapter.getData().size() / Integer.valueOf(Constants.PAGE_SIZE) + 1;

            }
        });
    }
}
